package v1

import (
	"gin-vue-admin/global"
	"gin-vue-admin/model"
	"gin-vue-admin/model/request"
	"gin-vue-admin/model/response"
	"gin-vue-admin/service"
	"gin-vue-admin/utils"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

// @Tags Train
// @Summary 创建Train
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body model.Train true "创建Train"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /train/createTrain [post]
func CreateTrain(c *gin.Context) {
	var train model.Train
	_ = c.ShouldBindJSON(&train)
	if err := service.CreateTrain(train); err != nil {
        global.GVA_LOG.Error("创建失败!", zap.Any("err", err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}

// @Tags Train
// @Summary 删除Train
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body model.Train true "删除Train"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
// @Router /train/deleteTrain [delete]
func DeleteTrain(c *gin.Context) {
	var train model.Train
	_ = c.ShouldBindJSON(&train)
	if err := service.DeleteTrain(train); err != nil {
        global.GVA_LOG.Error("删除失败!", zap.Any("err", err))
		response.FailWithMessage("删除失败", c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}

// @Tags Train
// @Summary 批量删除Train
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.IdsReq true "批量删除Train"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"批量删除成功"}"
// @Router /train/deleteTrainByIds [delete]
func DeleteTrainByIds(c *gin.Context) {
	var IDS request.IdsReq
    _ = c.ShouldBindJSON(&IDS)
	if err := service.DeleteTrainByIds(IDS); err != nil {
        global.GVA_LOG.Error("批量删除失败!", zap.Any("err", err))
		response.FailWithMessage("批量删除失败", c)
	} else {
		response.OkWithMessage("批量删除成功", c)
	}
}

// @Tags Train
// @Summary 更新Train
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body model.Train true "更新Train"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
// @Router /train/updateTrain [put]
func UpdateTrain(c *gin.Context) {
	var train model.Train
	_ = c.ShouldBindJSON(&train)
	if err := service.UpdateTrain(train); err != nil {
        global.GVA_LOG.Error("更新失败!", zap.Any("err", err))
		response.FailWithMessage("更新失败", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}

// @Tags Train
// @Summary 用id查询Train
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body model.Train true "用id查询Train"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /train/findTrain [get]
func FindTrain(c *gin.Context) {
	var train model.Train
	_ = c.ShouldBindQuery(&train)
	if err, retrain := service.GetTrain(train.ID); err != nil {
        global.GVA_LOG.Error("查询失败!", zap.Any("err", err))
		response.FailWithMessage("查询失败", c)
	} else {
		response.OkWithData(gin.H{"retrain": retrain}, c)
	}
}

// @Tags Train
// @Summary 分页获取Train列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.TrainSearch true "分页获取Train列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /train/getTrainList [get]
func GetTrainList(c *gin.Context) {
	var pageInfo request.TrainSearch
	_ = c.ShouldBindQuery(&pageInfo)
	if err, list, total := service.GetTrainInfoList(pageInfo); err != nil {
	    global.GVA_LOG.Error("获取失败", zap.Any("err", err))
        response.FailWithMessage("获取失败", c)
    } else {
        response.OkWithDetailed(response.PageResult{
            List:     list,
            Total:    total,
            Page:     pageInfo.Page,
            PageSize: pageInfo.PageSize,
        }, "获取成功", c)
    }
}

// 给动车设置路线
func AddTrainLines(c *gin.Context) {
	var trainLines request.AddTrainLinesInfo
	_ = c.ShouldBindJSON(&trainLines)
	if err := utils.Verify(trainLines, utils.TrainIdVerify); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := service.AddTrainLines(trainLines.Lines, trainLines.TrainId); err != nil {
		global.GVA_LOG.Error("添加失败!", zap.Any("err", err))
		response.FailWithMessage("添加失败", c)
	} else {
		response.OkWithMessage("添加成功", c)
	}
}